/**
	网络
 */
package zstack

import (
	"bc/util"
	"encoding/json"
	"fmt"
)

type network struct {

}
/**
	创建普通二层网络
 */
func (t *network) CreateL2NoVlanNetwork (body string) (string,error){

	oauth,err := Oauth()
	if err != nil{
		return "",err
	}
	url := util.Config.Zstack.ApiUrl + "v1/l2-networks/no-vlan"

	h := make(map[string]string)
	h["Content-Type"] = "application/json;charset=UTF-8"
	h["Authorization"] = "OAuth " + oauth
	h["X-Web-Hook"] = util.Config.Zstack.WebHook + "l2_net_add_call_back"

	result,err := util.Curl.Header(h).Body(body).Url(url).POST()
	return result,err

}
/**
	删除二层网络(DeleteL2Network)
 */
func (t *network)Delete(uuid string,del_mode string) (string,error){

	oauth,err := Oauth()
	if err != nil{
		return "",err
	}
	url := util.Config.Zstack.ApiUrl + "v1/l2-networks/%s?deleteMode=%s"
	url = fmt.Sprintf(url,uuid,del_mode)

	h := make(map[string]string)
	h["Content-Type"] = "application/json;charset=UTF-8"
	h["Authorization"] = "OAuth " + oauth
	h["X-Web-Hook"] = util.Config.Zstack.WebHook + "l2_net_del_call_back?uuid=" + uuid

	result,err := util.Curl.Header(h).Url(url).DELETE()
	return result,err

}
/**
	创建三级网络
 */
func (t *network)CreateL3Network(body string) (string,error) {
	oauth,err := Oauth()
	if err != nil{
		return "",err
	}
	url := util.Config.Zstack.ApiUrl + "v1/l3-networks"

	h := make(map[string]string)
	h["Content-Type"] = "application/json;charset=UTF-8"
	h["Authorization"] = "OAuth " + oauth
	h["X-Web-Hook"] = util.Config.Zstack.WebHook + "l3_net_add_call_back"

	result,err := util.Curl.Header(h).Url(url).Body(body).POST()
	return result,err
}
func (t *network)DeleteL3Network(uuid string,deleteMode string) (string,error) {

	oauth,err := Oauth()
	if err != nil{
		return "",err
	}
	url := util.Config.Zstack.ApiUrl + "v1/l3-networks/%s?deleteMode=%s"
	url = fmt.Sprintf(url,uuid,deleteMode)

	header := make(map[string]string)
	header["Content-Type"] = "application/json;charset=UTF-8"
	header["Authorization"] = "OAuth " + oauth
	header["X-Web-Hook"] = util.Config.Zstack.WebHook + "l3_net_del_call_back?uuid=" + uuid
	result,err := util.Curl.Header(header).Url(url).DELETE()
	return result,err

}
/**
 * 创建VXLAN网络(CreateL2VxlanNetwork)
 */
func (t *network)CreateL2VxlanNetwork(body string, sid int) (string,error) {
	oauth,err := Oauth()
	if err != nil{
		return "",err
	}
	url := util.Config.Zstack.ApiUrl + "v1/l2-networks/vxlan"

	header := make(map[string]string, 2)
	header["Content-Type"] = "application/json;charset=UTF-8"
	header["Authorization"] = "OAuth " + oauth
	header["X-Web-Hook"] = util.Config.Zstack.WebHook + "l2_net_add_call_back?sid=" + string(sid)
	result,err := util.Curl.Header(header).Url(url).Body(body).POST()
	return result,err
}
func (t *network)GetResults(url string) (r map[string]interface{}) {
	h := make(map[string]string)
	err := json.Unmarshal([]byte(url),&h)

	result,err := util.Curl.Url(h["location"]).GET()
	err = json.Unmarshal([]byte(result),&r)
	if err != nil {
		return r
	}
	return r
}
/**
创建二层VLAN网络
 */
func (t *network)CreateL2VlanNetwork(body string) (string,error) {
	oauth,err := Oauth()
	if err != nil{
		return "",err
	}
	url := util.Config.Zstack.ApiUrl + "v1/l2-networks/vlan"

	h := make(map[string]string)
	h["Content-Type"] = "application/json;charset=UTF-8"
	h["Authorization"] = "OAuth " + oauth
	h["X-Web-Hook"] = util.Config.Zstack.WebHook + "l2_net_add_call_back"

	result,err := util.Curl.Header(h).Url(url).Body(body).POST()
	return result,err
}
/**
修改二层网络
*/
func (t *network) UpdateL2Network (uuid string, body string) (string,error){

	oauth,err := Oauth()
	if err != nil{
		return "",err
	}
	url := util.Config.Zstack.ApiUrl + "v1/l2-networks/" + uuid + "/actions"

	h := make(map[string]string)
	h["Content-Type"] = "application/json;charset=UTF-8"
	h["Authorization"] = "OAuth " + oauth
	h["X-Web-Hook"] = util.Config.Zstack.WebHook + "l2_net_edit_call_back?uuid=" + uuid
	result,err := util.Curl.Header(h).Body(body).Url(url).PUT()
	return result,err

}

var NetWork = &network{}
